﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.MenxingManager
{
    public partial class MenxingList : Form
    {
        public MenxingList()
        {
            InitializeComponent();
            this.dgMenxing.AutoGenerateColumns = false;
        }
        
        private MenxingDAL dal = new MenxingDAL();
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            MenxingEditing me = new MenxingEditing();
            me.IsNew = true;
            if(me.ShowDialog()==DialogResult.OK){
                LoadData();
            }
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = this.dgMenxing.SelectedRows.Count;
            if (count < 1)
            {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            else
            {
                MenxingEditing me = new MenxingEditing();
                me.IsNew = false;
                me.EditingId = this.dgMenxing.SelectedRows[0].Cells["id"].Value.ToString();
                if (me.ShowDialog() == DialogResult.OK) {
                    LoadData();
                }
            }

        }
        //删除操作
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = this.dgMenxing.SelectedRows.Count;
            if (count < 1)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            else
            {
                string mx=this.dgMenxing.SelectedRows[0].Cells["mxbh"].Value.ToString();
                string id = this.dgMenxing.SelectedRows[0].Cells["id"].Value.ToString();
                string info= "您确定要删除"+mx+"门型么";
                if (MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) {
                    int result = dal.DelData(id);
                    if (result > 0)
                    {
                        MessageBox.Show("删除成功");
                        LoadData();
                    }
                    else {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MenxingList_Load(object sender, EventArgs e)
        {
            Dictionary<int, string> dicGlass = new Dictionary<int, string>();
            dicGlass.Add(0, "否");
            dicGlass.Add(1, "是");
            BindingSource bs = new BindingSource();
            bs.DataSource = dicGlass;
            this.glass.DataSource = bs;
            this.glass.DisplayMember = "value";
            this.glass.ValueMember = "key";

            Dictionary<int, string> dicXb = new Dictionary<int, string>();
            dicXb.Add(0, "否");
            dicXb.Add(1, "是");
            BindingSource bsxb = new BindingSource();
            bsxb.DataSource = dicXb;
            this.isspecial.DataSource = bsxb;
            this.isspecial.DisplayMember = "value";
            this.isspecial.ValueMember = "key";

            Dictionary<int, string> diclimit = new Dictionary<int, string>();
            diclimit.Add(0, "否");
            diclimit.Add(1, "是");
            BindingSource bslimit = new BindingSource();
            bslimit.DataSource = diclimit;
            this.limitcc.DataSource = bslimit;
            this.limitcc.DisplayMember = "value";
            this.limitcc.ValueMember = "key";


            LoadData();
        }
        private void LoadData() {
            string mxbh = this.tbmxbh.Text.Trim();
            string mxsm = this.tbmxsm.Text.Trim();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("mxbh",mxbh);
            dic.Add("mxsm",mxsm);



            this.dgMenxing.DataSource = dal.ListAll(dic);
        }

        private void MenxingList_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.ml = null;
        }
        //查询
        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
